<?php

namespace app\common\model;

use app\admin\library\Auth;
use fast\Tree;
use think\Db;
use think\Model;

class GoodsCategory extends Model
{
    // 表名
    protected $name = 'goods_category';
    protected $autoWriteTimestamp = true;
    
    // 追加属性
    protected $append = [
        'status_text',
        'create_time_text',
        'update_time_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['sort' => $row[$pk]]);
        });
    }

    
    public function getStatusList()
    {
        return ['1' => '启用', '0' => '禁用'];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
	
	/**
	 * 获取上下级关系分类
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function getTreeCategory(){
		$tree = Tree::instance();
		$tree->init(collection($this->order('sort desc,id desc')->select())->toArray(), 'parent_id');
		$categorys = $tree->getTreeList($tree->getTreeArray(0), 'name');
		return $categorys;
	}
	
	public function getListById($pid = 0){
		$where['parent_id'] = $pid;
		
		$rows = Db::name('goodsCategory')->where($where)->order('sort ASC')->select();
		
		return $rows;
	}
	
	/**
	 * 供应链商品分类检查， 不存在就添加
	 * @param $data
	 * @return int|mixed|string
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public function checkCategory($data){
		if(isset($data['ppcate'])){
			$cateRow = $this->where(['name' => $data['ppcate']['name'], 'level' => 1])->find();
			if($cateRow){
				$ppid = $cateRow['id'];
			} else {
				$ppid = $this->insertGetId([
					'parent_id' => 0,
					'name' => $data['ppcate']['name'],
					'parent_path' => '0',
					'image' => $data['ppcate']['image'],
					'level' => 1,
				]);
			}
			
			if(isset($data['pcate'])){
				$cateRow = $this->where(['name' => $data['pcate']['name'], 'level' => 2])->find();
				if($cateRow){
					$pid = $cateRow['id'];
				} else {
					$pid = $this->insertGetId([
						'parent_id' => $ppid,
						'name' => $data['pcate']['name'],
						'parent_path' => '0_'.$ppid,
						'image' => $data['pcate']['image'],
						'level' => 2,
					]);
				}
				
				if(isset($data['cate'])){
					$cateRow = $this->where(['name' => $data['cate']['name'], 'level' => 3])->find();
					if(!$cateRow){
						$cid = $this->insertGetId([
							'parent_id' => $pid,
							'name' => $data['cate']['name'],
							'parent_path' => '0_'.$ppid.'_'.$pid,
							'image' => $data['cate']['image'],
							'level' => 3,
						]);
					} else {
						$cid = $cateRow['id'];
					}
					
					return $cid;
				}
			}
		}
	}
}
